package br.com.educa.modelo.publico;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.TypedQuery;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.serializable.RooSerializable;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Table
@Configurable
@RooSerializable
@RooJson
@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findPaisesByPaisEquals" })
public class Pais implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    @NotNull
    private String pais;

    @PersistenceContext
    transient EntityManager entityManager;

    private static final long serialVersionUID = 1L;

    public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    public static final EntityManager entityManager() {
        EntityManager em = new Pais().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countPaises() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Pais o", Long.class).getSingleResult();
    }

    public static List<br.com.educa.modelo.publico.Pais> findAllPaises() {
        return entityManager().createQuery("SELECT o FROM Pais o", Pais.class).getResultList();
    }

    public static br.com.educa.modelo.publico.Pais findPais(Long id) {
        if (id == null) return null;
        return entityManager().find(Pais.class, id);
    }

    public static List<br.com.educa.modelo.publico.Pais> findPaisEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Pais o", Pais.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Pais attached = Pais.findPais(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public br.com.educa.modelo.publico.Pais merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Pais merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

    public static br.com.educa.modelo.publico.Pais fromJsonToPais(String json) {
        return new JSONDeserializer<Pais>().use(null, Pais.class).deserialize(json);
    }

    public static String toJsonArray(Collection<br.com.educa.modelo.publico.Pais> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

    public static Collection<br.com.educa.modelo.publico.Pais> fromJsonArrayToPaises(String json) {
        return new JSONDeserializer<List<Pais>>().use(null, ArrayList.class).use("values", Pais.class).deserialize(json);
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getVersion() {
        return this.version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public String getPais() {
        return this.pais;
    }

    public void setPais(String pais) {
        this.pais = pais;
    }

	public static TypedQuery<Pais> findPaisesByPaisEquals(String pais) {
        if (pais == null || pais.length() == 0) throw new IllegalArgumentException("The pais argument is required");
        EntityManager em = Pais.entityManager();
        TypedQuery<Pais> q = em.createQuery("SELECT o FROM Pais AS o WHERE o.pais = :pais", Pais.class);
        q.setParameter("pais", pais);
        return q;
    }
}
